package com.zhongxi.guoyuan.fragment.RewardCoin;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.RewardCommentAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.rewardcoin.MallCommentResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class RewardCoinCommentFragment extends BaseFragment {

    private String goodsid;
    private NoScrollListView lvList;
    private List<MallCommentResult.MallComment> data = new ArrayList<MallCommentResult.MallComment>();
    private RewardCommentAdapter adapter;

    public RewardCoinCommentFragment(String goodsid) {
        this.goodsid = goodsid;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_reward_coin_comment, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        lvList = (NoScrollListView) getView().findViewById(R.id.lvList);
        adapter = new RewardCommentAdapter(context, data);
        lvList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
        String str = "&gid="+goodsid+"&page="+pageNum;
        getServer(Url.MALL_COMMENTS, null, str, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            MallCommentResult result = gson.fromJson(response, MallCommentResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            if(pageNum == 1){
                data.clear();
            }
            if(null!=result.getComments()&&!"".equals(result.getComments())){
                data.addAll(result.getComments());
            }
            try {
                JSONObject obj = new JSONObject(response);
                JSONObject userObj = obj.getJSONObject("user");
                if (null != userObj && !"".equals(userObj)) {
                    adapter.setUser(userObj);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            adapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }
}
